import torch

# 验证
def validate(disp, ground_truth, img_name):
    # 掩码
    mask = (ground_truth > 0)
    # mask[:, :60] = False

    # 计算batch的误差
    epe = torch.abs(disp[mask] - ground_truth[mask]) # 预测视差和真实视差的差值
    error_avg = torch.mean(epe).item()
    error_1px = torch.sum((epe >= 1.0)).item() / torch.numel(disp[mask])
    error_2px = torch.sum((epe >= 2.0)).item() / torch.numel(disp[mask])
    error_3px = torch.sum((epe >= 3.0)).item() / torch.numel(disp[mask])
    error_4px = torch.sum((epe >= 4.0)).item() / torch.numel(disp[mask])
    error_5px = torch.sum((epe >= 5.0)).item() / torch.numel(disp[mask])

    return {'error_avg': error_avg,
            'error_1px': error_1px,
            'error_2px': error_2px,
            'error_3px': error_3px,
            'error_4px': error_4px,
            'error_5px': error_5px,
            'img_name': img_name}
